<?php
ob_start(); 
session_start();
function check_email_address($email) {
  // First, we check that there's one @ symbol, 
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
      return false;
    }
  }
  // Check if domain is IP. If not, 
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
function getlang(){
		$lang = $_SESSION['lang'];
		if (empty($lang) || $lang == "KR") {
			$value = "value/value_kr.php";
		} else if($lang == "VN") {
			$value = "value/value_vn.php";
		}
		else if($lang == "EN"){
			$value = "value/value_en.php";
		}
		return $value;
	}
	
	$value = getlang();
	include($value);


if(isset($_REQUEST['signup'])){
	//$emailrestore=$_REQUEST['emailrestore'];
	$email=$_REQUEST['email'];
	$passwd=$_REQUEST['passwd'];
	$cpasswd=$_REQUEST['cpasswd'];
	$code=$_REQUEST['code'];
	$captcha=$_REQUEST['captcha'];
	// check email
	session_unregister('error');
	session_unregister('success');
	
	if(empty($email))
			$_SESSION['error']=$errorSignup1;
	
	else if(check_email_address($email)){
		//check password
		
		if(empty($passwd))
			$_SESSION['error']=$errorSignup2;
		else if(empty($cpasswd) )
			$_SESSION['error']=$errorSignup3;
		/*else if(empty($emailrestore))
			$_SESSION['error']=$errorSignup9;
		else if(!check_email_address($emailrestore))
			$_SESSION['error']=$errorSignup10;
		else if($email==$emailrestore)
			$_SESSION['error']=$errorSignup11;*/
		else if($passwd==$cpasswd){
			//check captcha
			if($captcha=="" )
				$_SESSION['error']=$errorSignup4;
			else if(strcasecmp($code,$captcha)==0){
				include 'connection/connection.php';
				$c=mysql_query("INSERT INTO user_info (email,passwd) VALUES ('$email', '".md5($passwd)."')");
				if ($c==1){
					mysql_query("INSERT INTO license(email,id_type_license,status) VALUES ('$email', '1', '1')");
					$_SESSION['action']='dangnhap';
				}
				else 
					$_SESSION['error']=$errorSignup5;
				mysql_connect($connection);
			}else
				$_SESSION['error']=$errorSignup6;
		}else
			$_SESSION['error']=$errorSignup7;
	}	
	else
	    $_SESSION['error']=$errorSignup8;

	header( 'Location: index.php' );
}

ob_flush();
?>